package com.exam.qc.controller;


import com.exam.qc.model.PageVo;
import com.exam.qc.model.Product;
import com.exam.qc.model.ReturnVo;
import com.exam.qc.service.ProductService;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;


    /**
     * 分页查询产品信息接口
     * @param productName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/search",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ReturnVo listPage(
            @RequestParam(value = "productName",required = true,defaultValue = "")String productName,
            @RequestParam(value = "pageNo",required = true,defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize",required = true,defaultValue = "2") Integer pageSize

    ){
        PageVo<Product> productPageVo = productService.queryPage(productName,pageNo,pageSize);
        return ReturnVo.success(productPageVo);
    }


    /**
     * 查询所有产品列表接口
     * @return
     */
    @RequestMapping(value = "/list",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ReturnVo getProList(){
        List<Product> productList = productService.queryAll();
        return ReturnVo.success(productList);
    }

    /**
     * 判断日期格式是否正确
     * @param sDate
     * @return
     */
    private boolean isValidDate(String sDate)
    {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 新增产品接口
     * @param product
     * @return
     */
    @RequestMapping(value = "/save",consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ReturnVo saveProduct(@RequestBody  Product product){
        if(StringUtils.isBlank(product.getProductName())){
            return ReturnVo.success("产品名称为必填参数！");
        }
        else if(product.getProductDate() == null){
            return ReturnVo.success("生产日期为必填参数！");
        }
        else if(isValidDate(product.getProductDate().toString())){
            return ReturnVo.success("生产日期参数格式不正确，需符合“yyyy-MM-dd”格式！");
        }
        else {
            Integer retVal = productService.addProduct(product);
            if (retVal > 0) {
                return ReturnVo.success("保存产品信息成功！");
            } else {
                return ReturnVo.success("保存产品信息失败！");
            }
        }
    }



}
